<?php

/**
 * This is the model class for table "user_back".
 *
 * The followings are the available columns in table 'user_back':
 * @property integer $user_id
 * @property string $user_name
 * @property string $password
 * @property string $first_name
 * @property string $last_name
 * @property integer $sex
 * @property string $avata_url
 * @property string $birthday
 * @property string $email
 * @property string $country_code
 * @property integer $city_id
 * @property string $register_date
 * @property string $last_visit_date
 * @property string $last_login_ip
 * @property string $activation_code
 * @property string $group_code
 * @property string $status
 * @property string $website
 */
class UserBack extends CActiveRecord {
    public $phone, $yahoo, $avatar, $address, $confirm_password, $categories;

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'user_back';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(

            array('first_name', 'required', 'message' => 'Vui lòng nhập tên'),
            array('last_name', 'required', 'message' => 'Vui lòng nhập họ'),
            array('user_name', 'required', 'message' => 'Vui lòng nhập user name'),
            array('password', 'required', 'message' => 'Vui lòng nhập mật khẩu'),
            array('confirm_password', 'required', 'message' => 'Vui lòng nhập nhập xác nhận mật khẩu', 'on' => array('insert')),
            array('email', 'required', 'message' => 'Vui lòng nhập email'),

            array('email', 'email', 'message' => 'Email không hợp lệ'),
            array('email', 'checkUniqueEmail', 'on' => array('insert')),
            array('user_name', 'checkUniqueUserName', 'on' => array('insert')),
            array('sex, city_id', 'numerical', 'integerOnly' => true),
            array('user_name, first_name, last_name, avata_url, email, activation_code', 'length', 'max' => 127),
            array('password', 'length', 'max' => 100),
            array('password', 'compare', 'compareAttribute' => 'confirm_password', 'on' => array('insert'), 'message' => 'Xác nhận mật khẩu không trùng khớp'),

            array('country_code', 'length', 'max' => 2),
            array('last_login_ip, group_code', 'length', 'max' => 20),
            array('birthday, register_date, last_visit_date', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('user_id, user_name, password, first_name, last_name, sex, avata_url, birthday, email, country_code, city_id, register_date, last_visit_date, last_login_ip, activation_code, group_code,status,website', 'safe', 'on' => 'search'),
        );
    }

    public function checkUniqueEmail() {
        $user =
            $this->findAll(array(
                'select' => array('email'),
                'condition' => 'LOWER(TRIM(email)) = :email',
                'params' => array(':email' => strtolower(trim($this->email)))
            ));
        if ($user != null) {
            $this->addError($this->email, "Email này đã được sử dụng.");
        }
    }

    public function checkUniqueUsername() {
        $user =
            $this->findAll(array(
                'select' => array('user_name'),
                'condition' => 'LOWER(TRIM(user_name)) = :name',
                'params' => array(':name' => strtolower(trim($this->user_name)))
            ));
        if ($user != null) {
            $this->addError($this->email, "Tên tài khoản này đã được sử dụng.");
        }
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array();
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'user_id' => 'User',
            'user_name' => 'User Name',
            'password' => 'Mật khẩu',
            'first_name' => 'Tên',
            'last_name' => 'Họ',
            'sex' => 'Giới tính',
            'avata_url' => 'Avata Url',
            'birthday' => 'Birthday',
            'email' => 'Email',
            'country_code' => 'Country Code',
            'city_id' => 'City',
            'register_date' => 'Register Date',
            'last_visit_date' => 'Last Visit Date',
            'last_login_ip' => 'Last Login Ip',
            'activation_code' => 'Activation Code',
            'group_code' => 'Group Code',
            'phone' => 'Số điện thoại',
            'yahoo' => 'Tài khoản yahoo',
            'avatar' => 'Ảnh đại diện',
            'address' => 'Địa chỉ',
            'confirm_password' => 'Xác nhận mật khẩu',
            'status' => 'Kích hoạt',
            'website' => 'Website',
            'categories' => 'Danh mục ưu thích'
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search() {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('user_id', $this->user_id);
        $criteria->compare('user_name', $this->user_name, true);
        $criteria->compare('password', $this->password, true);
        $criteria->compare('first_name', $this->first_name, true);
        $criteria->compare('last_name', $this->last_name, true);
        $criteria->compare('sex', $this->sex);
        $criteria->compare('avata_url', $this->avata_url, true);
        $criteria->compare('birthday', $this->birthday, true);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('country_code', $this->country_code, true);
        $criteria->compare('city_id', $this->city_id);
        $criteria->compare('register_date', $this->register_date, true);
        $criteria->compare('last_visit_date', $this->last_visit_date, true);
        $criteria->compare('last_login_ip', $this->last_login_ip, true);
        $criteria->compare('activation_code', $this->activation_code, true);
        $criteria->compare('group_code', $this->group_code, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'pagination' => array('pageSize' => Yii::app()->params['backendPageSize'])
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return UserBack the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }
}
